package com.myapp.ruyixing.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.myapp.ruyixing.R;
import com.myapp.ruyixing.ui.widget.TitleBar;

public class MessageActivity extends BaseActivity implements
		OnPageChangeListener, OnClickListener {

	private TitleBar mTitleBar;
	private ViewPager mViewPager;
	
	private TextView mNotifyTab;
	private TextView mDMTab;
	private TextView mResponseTab;
	
	private View mCurrentTab;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.activity_message);

		initView();
	}

	private void initView() {
		mTitleBar = (TitleBar) findViewById(R.id.title_bar);
		mViewPager = (ViewPager) findViewById(R.id.view_pager);
		mViewPager.setOnPageChangeListener(this);
		mViewPager.setAdapter(new PagerAdapter(getSupportFragmentManager()));
		
		mNotifyTab = (TextView) findViewById(R.id.notify);
		mDMTab = (TextView) findViewById(R.id.dm);
		mResponseTab = (TextView) findViewById(R.id.response);
		
		mNotifyTab.setOnClickListener(this);
		mDMTab.setOnClickListener(this);
		mResponseTab.setOnClickListener(this);
		
		mCurrentTab = mNotifyTab;
		mCurrentTab.setSelected(true);

		mTitleBar.setOnIconClickedListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.notify:
			if (mCurrentTab == v) {
				return;
			}
			selectTab(mCurrentTab);
			mViewPager.setCurrentItem(0);
			
			break;
		case R.id.dm:
			if (mCurrentTab == v) {
				return;
			}
			selectTab(mCurrentTab);
			mViewPager.setCurrentItem(1);
			
			break;
		case R.id.response:
			if (mCurrentTab == v) {
				return;
			}
			selectTab(mCurrentTab);
			mViewPager.setCurrentItem(2);
			
			break;

		default:
			break;
		}
		
	}
	
	private void selectTab(View v) {
		mCurrentTab.setSelected(false);
		mCurrentTab = v;
		mCurrentTab.setSelected(true);
	}

	private class PagerAdapter extends FragmentStatePagerAdapter {
		public PagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public int getCount() {
			return 3;
		}

		@Override
		public Fragment getItem(int position) {
			switch (position) {
			case 0:
				return new NotifyFragment();

			case 1:
				return new DirectMessageFragment();

			case 2:
				return new ReplyFragment();

			default:
				return null;
			}
		}

	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	@Override
	public void onPageSelected(int arg0) {
		if (arg0 == 0) {
			selectTab(mNotifyTab);
		} else if (arg0 == 1) {
			selectTab(mDMTab);
		} else if (arg0 == 2) {
			selectTab(mResponseTab);
		}
	}
}
